package com.alks.function.controller.oa;

import com.alibaba.fastjson2.JSON;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.oa.OaStyle;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.oa.OaStyleReq;
import com.alks.function.mapper.oa.OaStyleMapper;
import com.alks.function.service.oa.OaStyleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * @author:hxl
 * @create: 2024-09-10 13:59
 * @Description:
 */
@Api(tags = "OA建款")
@Slf4j
@ApiRestController("/oaStyle")
public class OaStyleController {
    @Autowired
    private OaStyleService oaStyleService;

    @PostMapping("/getOaStyle")
    @WebLogger
    @ApiOperation(value = "获取OA建款")
    public ResponseInfo getOaStyle(@RequestBody OaStyleReq oaStyleReq) {
        return oaStyleService.getOaStyle(oaStyleReq);
    }

    @PostMapping("/createOaStyle")
    @WebLogger
    @ApiOperation(value = "新建")
    public ResponseInfo createOaStyle(OaStyle oaStyle){
    	return oaStyleService.createOaStyle(oaStyle);
    }

    @PostMapping("/updateOaStyle")
    @WebLogger
    @ApiOperation(value = "修改")
    public ResponseInfo updateOaStyle(OaStyle oaStyle){
    	return oaStyleService.updateOaStyle(oaStyle);
    }

    @PostMapping("/deleteOaStyle")
    @WebLogger
    @ApiOperation(value = "删除")
    public ResponseInfo deleteOaStyle(OaStyle oaStyle){
        return oaStyleService.deleteOaStyle(oaStyle);
    }

    @Autowired
    private OaStyleMapper oaStyleMapper;

    @PostMapping("/downloadOaStyle")
    @WebLogger
    @ApiOperation("导出")
    public void downloadOaStyle(HttpServletResponse response, @RequestBody List<String> ids) throws IOException {
        List<OaStyle> list = oaStyleMapper.getOaStyleByIds(ids);
        for (OaStyle oaStyle : list) {
            List<String> photos = JSON.parseObject(oaStyle.getPhotoPath(), List.class);
            if (photos.size() == 1){
                oaStyle.setUrl(new URL(photos.get(0)));
            }
        }
        String name = "款号库-"+UserIdThread.get().getUserName()+"-"+DateUtils.dateTimeNow();
        ExcelUtils.creatByTemplate(response, "款号库模板.xlsx",name,list);
    }
}
